<template>
<div>
      <!-- 顶部主键 -->
      <div class="gotop-box go-top" @click="handleTop" v-if="btnFlag">
        <a href="#">
          <i class="iconfont icon-shangjiantouxiangshang-1-copy"></i>
        </a>
      </div>
  </div>
</template>

<script>
export default {
    data() {
        return {
             scrollTop: 0,
            btnFlag: false,
        }
    },
  mounted() {
    window.addEventListener("scroll", this.scrollToTop,true);
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollToTop);
  },
  methods: {
    handleTop() {
      const that = this;
      let timer = setInterval(() => {
        let ispeed = Math.floor(-that.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          that.scrollTop + ispeed;
        if (that.scrollTop === 0) {
          clearInterval(timer);
        }
      },10);
    },
    scrollToTop() {
      const that = this;
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      that.scrollTop = scrollTop;
      if (that.scrollTop > 120) {
        that.btnFlag = true;
      } else {
        that.btnFlag = false;
      }
    },
  },
};
</script>
<style lang="less" scoped>
  .gotop-box {
    width: 30px;
    height: 30px;
    background: rgba(0, 191, 157, 0.7);
    border-radius: 50%;
    line-height: 30px;
    text-align: center;
    position: fixed;
    right: 0;
    bottom: 15%;
    i {
      color: #fff;
      font-size: 20px;
      font-weight: 600;
    }
  }

</style>